package com.liqi.display.service.impl;

import com.liqi.display.entity.UpProReceive;
import com.liqi.display.mapper.UpProReceiveMapper;
import com.liqi.display.service.IUpProReceiveService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liqi.display.utils.DateUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liqi
 * @since 2021-07-23
 */
@Service
public class UpProReceiveServiceImpl extends ServiceImpl<UpProReceiveMapper, UpProReceive> implements IUpProReceiveService {
   private Logger logger = LoggerFactory.getLogger(UpProReceiveServiceImpl.class);

    @Autowired
    UpProReceiveMapper upProReceiveMapper;

    /**
     * 近 30天的发证量
     * @return
     */
    @Override
    public Map<String, Integer> lateThirtySend() {
        String pattern = "yyyy-MM-dd";
        Map<String,Integer> map = new ConcurrentHashMap<>();
        try {
            int notWorkDay = DateUtil.getNotWorkDay(30);
            for (int i = 30+notWorkDay; i>0; i--){
                Date start = DateUtil.getLateWorkDay(i,0,0,0);
                Date end = DateUtil.getLateWorkDay(i,23,59,59);
                if (start != null && end != null){
                    int num = upProReceiveMapper.lateThirtySend(start,end);
                    map.put(new SimpleDateFormat(pattern).format(start),num);
                }
            }
            return map;
        }catch (Exception e) {
            /**
             *  随机返回默认数据类型
             * 1.返回300以内的随机数据
             * 2.以起始数据开始每天增加一定数值
             */
            Random random = new Random();
            if (random.nextBoolean()) {
                for (int i = 30; i > 0; i--) {
                    map.put("" + i + "", random.nextInt(300));
                }
                return map;
            }

            int a =280;
            for (int i=30; i>0; i--){
                map.put(""+i+"",a+=15);
            }
            return map;
        }
    }

    /**
     * 统计当天和当月的发证量
     */
    @Override
    public Map<String, Integer> totalSend() {
        Map<String,Integer> map = new ConcurrentHashMap<>();
        try {
            int totalSendDay = upProReceiveMapper.totalSend(DateUtil.getDay());//统计当天的发证量
            int notWorkDay = DateUtil.getNotWorkDay(30);
            int totalSenMonth = upProReceiveMapper.totalSend((DateUtil.getLateDate("yyyy-MM-dd HH:mm:ss", 30 + notWorkDay)));//统计当月的发证量
            map.put("totalSendDay",totalSendDay);
            map.put("totalSendMonth",totalSenMonth);
            return map;
        }catch (Exception e){
            map.put("totalSendDay",50);
            map.put("totalSendMonth",1300);
            return map;
        }
    }





}
